/*
 * @Author: your name
 * @Date: 2021-09-10 16:03:28
 * @LastEditTime: 2021-10-18 16:54:11
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \autotestplant_frontend\src\router\index.js
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/users/Users.vue'


Vue.use(VueRouter)

const routes = [
  {path: '/',redirect:'/login'},
  {path: '/login',component: Login},
  // 这是一个包含子路由的写法
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {path: '/welcome',component: Welcome},              // 欢迎页
      {path: '/request_config',component: Welcome},       // 请求配置管理
      {path: '/server_config',component: Welcome},        // 服务配置
      {path: '/environment_config',component: Welcome},   // 环境配置
      {path: '/data_config',component: Welcome},          // 数据配置
      {path: '/ci_cd',component: Welcome},                // 持续集成
      {path: '/test_report',component: Welcome},          // 测试报告
      {path: '/user_list',component: Users},              // 用户管理
      {path: '/auth',component: Welcome},                 // 权限管理
      {path: '/platform_config',component: Welcome},      // 平台配置管理
      {path: '/operation_log',component: Welcome},        // 操作记录管理
    ]
  },
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  // to 将要访问的路径
  // from 代表从那个路径跳转而来
  // next 是一个函数,表示放行
  //    next()放行 next('/login')强制跳转到某个路由
  if (to.path==='/login') return next();
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr)  return next('/login')
  next()
})


export default router
